package com.global.college.ui.fragment.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseFragment;
import com.global.college.ui.activity.BigImageActivity;
import com.global.college.ui.activity.HsLagelDetailActivity;
import com.global.college.ui.activity.InviteActivity;
import com.global.college.ui.activity.LoginByPswActivity;
import com.global.college.ui.activity.MyCourseActivity;
import com.global.college.ui.activity.OrderCenterActivity;
import com.global.college.ui.activity.SettingActivity;
import com.global.college.ui.activity.TrainingCampActivity;
import com.global.college.ui.activity.UserCenterActivity;
import com.global.college.ui.bean.UploadHeaderModel;
import com.global.college.ui.bean.UserDataModel;
import com.global.college.ui.interfaces.IUserInfoView;
import com.global.college.ui.presenter.UserInfoPresenter;
import com.global.college.utils.CommonUtils;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.widget.FrescoImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 我的
 */
public class MineFragment extends BaseFragment implements IUserInfoView {

    @BindView(R.id.tv_app_version)
    TextView tvAppVersion;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.tv_training_num)
    TextView tvTrainingNum;
    @BindView(R.id.ll_trainning)
    LinearLayout llTrainning;
    @BindView(R.id.tv_course_num)
    TextView tvCourseNum;
    @BindView(R.id.ll_course)
    LinearLayout llCourse;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.ll_order)
    RelativeLayout llOrder;
    @BindView(R.id.fiv_avater)
    FrescoImageView fivAvater;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.iv_set)
    ImageView ivSet;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.ll_logined)
    LinearLayout llLogined;
    @BindView(R.id.ll_login)
    LinearLayout llLogin;
/*    @BindView(R.id.ll_more_info)
    LinearLayout llMoreInfo;*/
    Unbinder unbinder;
    private UserInfoPresenter userInfoPresenter;
    private UserDataModel mUserDataModel;

    @Override
    public int getContentViewId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        userInfoPresenter = new UserInfoPresenter(mActivity);
        userInfoPresenter.setUserInfoViewView(this);
    }

    @Override
    protected void initData() {
        if (StringUtils.isNotEmpty(UserData.getInstance().getToken(),true)){
            llLogined.setVisibility(View.VISIBLE);
//            llMoreInfo.setVisibility(View.VISIBLE);
            llLogin.setVisibility(View.GONE);
        }else {
            llLogined.setVisibility(View.GONE);
//            llMoreInfo.setVisibility(View.GONE);
            llLogin.setVisibility(View.VISIBLE);
        }

        tvAppVersion.setText("V " + CommonUtils.getVersion(mActivity));

    }

    @Override
    public void onResume() {
        super.onResume();
        userInfoPresenter.getUserInfo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_invite, R.id.ll_setting, R.id.ll_trainning, R.id.ll_course, R.id.ll_order, R.id.fiv_avater, R.id.ll_logined, R.id.ll_login,R.id.line_not_paid,R.id.line_completed,R.id.line_cancelled,
            R.id.ll_app_version,R.id.rela_user_agreement,R.id.rela_privacy_policy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_invite:
                JugeAndOpenActivity(InviteActivity.class);
                break;
            case R.id.ll_setting:
                JugeAndOpenActivity(SettingActivity.class);
                break;
            case R.id.ll_trainning:
                JugeAndOpenActivity(TrainingCampActivity.class);
                break;
            case R.id.ll_course:
                JugeAndOpenActivity(MyCourseActivity.class);
                break;
            case R.id.ll_order:
                JugeAndOpenActivity(OrderCenterActivity.class);
                break;
            case R.id.fiv_avater:
                if (null != mUserDataModel && StringUtils.isNotEmpty(mUserDataModel.getAvatar(),true)) {
                    List<String> mUrl = new ArrayList<>();
                    mUrl.add(mUserDataModel.getAvatar());
                    String[] strings = new String[mUrl.size()];
                    mUrl.toArray(strings);
                    Intent intent = new Intent();
                    intent.setClass(mActivity, BigImageActivity.class);
                    intent.putExtra("images", strings);
                    intent.putExtra("page", 0);
                    startActivity(intent);
                }
                break;
            case R.id.ll_logined:
                JugeAndOpenActivity(UserCenterActivity.class);
                break;
            case R.id.ll_login:
                startActivity(new Intent(mActivity, LoginByPswActivity.class));
                break;
            case R.id.line_not_paid:
                startActivity(new Intent(mActivity, OrderCenterActivity.class).putExtra("currentPosition", 1));
                break;
            case R.id.line_completed:
                startActivity(new Intent(mActivity, OrderCenterActivity.class).putExtra("currentPosition", 2));
                break;
            case R.id.line_cancelled:
                startActivity(new Intent(mActivity, OrderCenterActivity.class).putExtra("currentPosition", 3));
                break;
            case R.id.ll_app_version:
                ToastUtils.showCenterToast(getString(R.string.already_latest_version));
                break;
            case R.id.rela_user_agreement: //用户协议
                startActivity(new Intent(mActivity, HsLagelDetailActivity.class).putExtra("type", "0"));
                break;
            case R.id.rela_privacy_policy: //隐私政策
                startActivity(new Intent(mActivity, HsLagelDetailActivity.class).putExtra("type", "1"));
                break;

  /*          case R.id.ll_more_info:
                JugeAndOpenActivity(UserCenterActivity.class);
                break;*/
        }
    }

    @Override
    public void onGetUserInfoSuccess(UserDataModel userDataModel) {
        UserData.getInstance().setUserModel(userDataModel);
        mUserDataModel = userDataModel;
        if (null != userDataModel){
            tvNickname.setText(StringUtils.isNotEmpty(userDataModel.getNickName(), true) ? userDataModel.getNickName() : "暂无");;
            tvAccount.setText(StringUtils.isNotEmpty(userDataModel.getUserName(), true) ? "账号："+userDataModel.getUserName() : "账号：暂无");
            switch (userDataModel.getSex()){
                case "0":
                    ivSet.setVisibility(View.VISIBLE);
                    ivSet.setImageResource(R.mipmap.icon_user_sex_m);
                    break;
                    case "1":
                        ivSet.setVisibility(View.VISIBLE);
                        ivSet.setImageResource(R.mipmap.icon_user_sex_w);
                    break;
                default:
                    ivSet.setVisibility(View.GONE);
                    break;
            }
            if (StringUtils.isNotEmpty(userDataModel.getAvatar(), true)) {
                fivAvater.setImageURI( userDataModel.getAvatar());
            } else {
                fivAvater.setImageURI(Uri.parse("res:///" + R.mipmap.icon_user_my_header));
            }
        }
    }

    @Override
    public void onGetUserInfoFail(String error) {

    }

    @Override
    public void onSetUserInfoSuccess(Object smsModel) {

    }

    @Override
    public void onSetUserInfoFail(String error) {

    }

    @Override
    public void onUploadHeaderSuccess(UploadHeaderModel smsModel) {

    }

    @Override
    public void onUploadHeaderFail(String error) {

    }
}
